package com.bj.dljd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bj.dljd.domain.UserAccount;
import com.bj.dljd.domain.UserInfo;
import com.bj.dljd.enums.PublicStatus;
import com.bj.dljd.excepyions.BossException;
import com.bj.dljd.mapper.UserAccountMapper;
import com.bj.dljd.mapper.UserInfoMapper;
import com.bj.dljd.models.query.UserEditQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.service.UserService;
import com.bj.dljd.util.IdUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserAccountMapper userAccountMapper;


    @Override
    public Page<UserInfo> pageList(Integer pageNo, Integer pageSize) {

        Page<UserInfo> createTime = userInfoMapper.selectPage(Page.of(pageNo, pageSize)
                , new QueryWrapper<UserInfo>().orderByDesc("create_time"));


        return createTime;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized  void editUser(UserEditQuery userEditQuery) {

        // 判断当前注册还是修改
        if (StrUtil.isBlank(userEditQuery.getUserNo())) {
            // 业务逻辑要求 loginName 字段值不能重复
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name", userEditQuery.getLoginName());
            Long l = userInfoMapper.selectCount(wrapper);
            if (l > 0) { // 说明登录名称在数据库已存在
                throw new BossException(
                        R.builder().msg("登录名已存在，请更换其他").code(301).build());

            }
            // 注册商户 同时创建资金账户
            addUserInfoAndAccount(userEditQuery);

        } else {
            // 修改商户
            UserInfo userInfo = querByUserNo(userEditQuery.getUserNo());
            if (userInfo == null) {
                throw new BossException(R.fuil("无此用户"));
            }
            UpdateWrapper<UserInfo> wrapper = new UpdateWrapper<>();
            wrapper
            .set("shop_scope", userEditQuery.getShopScope())
            .set("phone", userEditQuery.getPhone())
            .set("shop_name", userEditQuery.getShopName())
            .set("user_email", userEditQuery.getUserEmail())
            .eq("id",userInfo.getId());
            userInfoMapper.update(wrapper);

        }
    }

    @Override
    public UserInfo querByUserNo(String userNo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no", userNo);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);


        return userInfo;
    }

    @Override
    public void modifyStatusByUserNo(String userNo) {
        // 先查询用户当前状态
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>()
                .eq("user_no", userNo));
        if (userInfo == null){
            throw new BossException(R.fuil("无此商户"));
        }
        String status = PublicStatus.ENABLE.name();
        if (PublicStatus.ENABLE.name().equals(userInfo.getStatus())){
            status = PublicStatus.DISABLE.name();
        }
        UpdateWrapper<UserInfo> wrapper = new UpdateWrapper();
        wrapper.eq("id", userInfo.getId())
                .set("status", status);
        userInfoMapper.update(wrapper);
    }






    // 注册商户和资金账号
    public void addUserInfoAndAccount(UserEditQuery userEditQuery) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(IdUtil.id());
        userInfo.setUserNo(IdUtil.buildUserNo());
        userInfo.setLoginName(userEditQuery.getLoginName());
        userInfo.setAccountNo(IdUtil.buildAccountNo());
        userInfo.setPhone(userEditQuery.getPhone());
        userInfo.setPassword(IdUtil.uuid());
        userInfo.setPayPwd(IdUtil.uuid());
        userInfo.setShopName(userEditQuery.getShopName());
        userInfo.setShopScope(userEditQuery.getShopScope());
        userInfo.setUserEmail(userEditQuery.getUserEmail());
        userInfo.setStatus(PublicStatus.ENABLE.name());
        userInfo.setCreateTime(new Date());
        userInfo.setRemark("创建离线用户");
        userInfoMapper.insert(userInfo);
        // 创建资金账号
        UserAccount userAccount = new UserAccount();
        userAccount.setId(IdUtil.id());
        userAccount.setUserNo(userInfo.getUserNo());
        userAccount.setAccountNo(userInfo.getAccountNo());
        userAccount.setBalance(new BigDecimal(0));
        userAccount.setTotalIncome(new BigDecimal(0));
        userAccount.setTotalExpend(new BigDecimal(0));
        userAccount.setStatus(PublicStatus.ENABLE.name());
        userAccount.setRemark("创建资金账号");
        userAccount.setEditTime(new Date());
        userAccountMapper.insert(userAccount);

    }



}
